/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Constraint/IMPCRepository.h"
#include "DBManager/Repository/HDF5/Base/SetRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace MPC
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64  SID;
        SG::DataStructure::Common::I64  G;
        SG::DataStructure::Common::I64  C;
        SG::DataStructure::Common::Real A;
        SG::DataStructure::Common::I64  GCA_POS;  // Start position of the rest G, C and A in GCA dataset
        SG::DataStructure::Common::I64  GCA_LEN;  // Length in GCA dataset
        SG::DataStructure::Common::I64  DOMAIN_ID;
    };
};  // namespace MPC
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::MPCData::MPCComponent> ()
{
    HighFive::CompoundType compoundType = {
        { "G", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "C", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "A", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
    };
    return compoundType;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::MPC::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "SID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "G", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "C", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "A", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "GCA_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GCA_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class MPCRepository : public IMPCRepository, public SetRepositoryBase<SG::DataStructure::FEM::MPCData>
{
  public:
    explicit MPCRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset
    };

    void InitialDatasetInfo () override;

    void DumpWriteCahce () override;

    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    void SortData () override;

    /**
     * @brief   : 根据sid查找数据
     * @param   : sid
     * @param   : arrs
     * @return  :
     */
    SG::DataStructure::Common::Status FindBySId (SG::DataStructure::Common::Id                      sid,
                                                 std::vector<SG::DataStructure::FEM::MPCData>& _OUT arrs) override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::MPCData>&                    own,
                       std::vector<SG::DBManager::HDF5::MPC::IDENTITY>& _OUT            IDENTITYData,
                       std::vector<SG::DataStructure::FEM::MPCData::MPCComponent>& _OUT subData,
                       bool                                                             isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITY
     * @param   : INFO
     * @param   : own
     */
    void NastranToOwn (std::vector<SG::DBManager::HDF5::MPC::IDENTITY>&            IDENTITYData,
                       std::vector<SG::DataStructure::FEM::MPCData::MPCComponent>& subData,
                       std::vector<SG::DataStructure::FEM::MPCData>& _OUT          own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
